package com.example.zsq.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.IntDef;
import android.widget.Button;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by ZSQ on 2017/9/25.
 */

public class MucisService extends Service{
    static MediaPlayer mp;
    int i =0;
    private boolean isStop;
    static  int flag =0;
    public void onCreate() {
        super.onCreate();
        new Thread(new DownThread()).start();
        mp = new MediaPlayer();
       // mp = MediaPlayer.create(this, R.raw.gequ);
        }
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }




    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String num = intent.getStringExtra("switch");
        int sum = intent.getIntExtra("stop", 0);
        int chang = intent.getIntExtra("ischang", 0);
        System.out.println("55555555" + num);
        System.out.println("-----------" + chang);
        System.out.println("555555555");
        //mp.start();
        if(chang !=0 )
            mp.seekTo(chang);
        else
            flag = 0;
        if (sum == 0) {

            try {
                mp.reset();
                mp.setDataSource(num);
                System.out.println("55555555" + num);
                mp.prepareAsync();
                mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                    public void onPrepared(MediaPlayer mp) {


                        mp.start();//开始音频
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if(sum == 1)
        {
            if(mp.isPlaying())
                mp.pause();

            else
                mp.start();
        }

        else
           mp.seekTo(sum);



            return super.onStartCommand(intent, flags, startId);
        }



    public String getTime() {                        //将毫秒转换成时间
        int total = mp.getDuration();
        String format = "mm:ss";                     //自定义时间模式
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(total);
    }
    public String getNOWTime() {                        //将毫秒转换成时间
        int total = mp.getCurrentPosition();
        String format = "mm:ss";                     //自定义时间模式
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(total);
    }

    class DownThread implements Runnable{

        public void run() {
            while (!isStop)
            {
                if(flag == 0) {
                    try {
                        i++;
                        Thread.sleep(1000);
                        Message msg = new Message();
                        int progress = (int) (mp.getCurrentPosition() * 1000.0 / mp.getDuration());
                        System.out.println("66666666666600000000000" + i);
                        msg.obj = getTime();
                        msg.arg1 = mp.getDuration();
                        msg.arg2 = mp.getCurrentPosition();
                        msg.what = progress;
                        MainActivity.handler2.sendMessage(msg);

                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else
                    System.out.println("zzzzzzzzzzzzzzzzzzzzzzz" );

            }
        }
    }
}
